﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using System.Drawing;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_LightHeal : TargetSpell 
    {
        //Pet only heal
        private int Rank1UnholyHealLow = 200;
        private int Rank1UnholyHealHigh = 230;

        private int Rank2UnholyHealLow = 160;
        private int Rank2UnholyHealHigh = 190;

        private int Rank3UnholyHealLow = 160;
        private int Rank3UnholyHealHigh = 190;


        //Level 1
        private int Rank1HealLow = 175;
        private int Rank1HealHigh = 210;

        //Level 11
        private int Rank2HealLow = 230;
        private int Rank2HealHigh = 260;

        //Level 21
        private int Rank3HealLow = 350;
        private int Rank3HealHigh = 400;


        public int LowValue
        {
            get
            {
                if (spellIcon.Name.IndexOf("Rank 3") > -1)
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank3HealLow;
                    else
                        return Rank3UnholyHealLow;
                }
                else if (spellIcon.Name.IndexOf("Rank 2") > -1)
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank2HealLow;
                    else
                        return Rank2UnholyHealLow;
                }
                else
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank1HealLow;
                    else
                        return Rank1UnholyHealLow;
                }
            }
        }
        public int HighValue
        {
            get
            {
                if (spellIcon.Name.IndexOf("Rank 3") > -1)
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank3HealHigh;
                    else
                        return Rank3UnholyHealHigh;
                }
                else if (spellIcon.Name.IndexOf("Rank 2") > -1)
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank2HealHigh;
                    else
                        return Rank2UnholyHealHigh;
                }
                else
                {
                    if (spellIcon.Skill == "Holy")
                        return Rank1HealHigh;
                    else
                        return Rank1UnholyHealHigh;
                }
            }
        }

        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            if (sIcon.Skill == "Holy")
                return true;

            return false;
        }

        private List<Point> hiddenList;
        private CreatureBase choosenTarget;

        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<Point> hiddenList)
        {
            this.hiddenList = hiddenList;

            List<CreatureBase> targetCreatures = new List<CreatureBase>();
            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                {
                    if (creatureBase.Faction == creature.Faction && creatureBase.HP < creatureBase.MaxHP)
                        targetCreatures.Add(creatureBase);
                }
            }

            int damagedAmount = 0; 
            foreach (CreatureBase cb in targetCreatures)
            {
                if (CreatureAI.isHidden(cb.MapX, cb.MapY, hiddenList) == false)
                {
                    int damageValue = cb.MaxHP - cb.HP;
                    if (choosenTarget == null || damageValue > damagedAmount)
                    {
                        choosenTarget = cb;
                        damagedAmount = damageValue;
                    }
                }
            }

            if (choosenTarget == null)
                return false;
            else
                return true;
        }


        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (spellIcon.Skill == "Unholy" && TargetCreatureBase.petController == null)
            {
                myCreatureBase.SendMessage("Invalid target.  Only a pet may be the target of " + spellIcon.Name, "server");
                if (myCreatureBase is Character)
                {
                    Character myChar = (Character)myCreatureBase;
                    myChar.PlaySound("Ding", myChar.MapX, myChar.MapY);
                }

                return;
            }

            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            int healAmt = Managers.GameHandler.random.Next(LowValue, HighValue + 1);;

            healAmt = SkillManager.AdjustForCombat(CastLevel, healAmt, CastingCreatureWis);
            TargetCreatureBase.HP = TargetCreatureBase.HP + healAmt;

            //int missingHP = TargetCreatureBase.MaxHP - TargetCreatureBase.HP;
            //int healAmt = System.Convert.ToInt32(missingHP * .5);
            //TargetCreatureBase.HP = TargetCreatureBase.HP + healAmt;

            TargetCreatureBase.Update_Character();

            if (myCreatureBase is Character)
            {
                Character myCharacter = (Character)myCreatureBase;

                Message healMsg1 = new Message();
                healMsg1.CreatureID = myCharacter.CreatureID;
                healMsg1.Type = "spelldefense";
                healMsg1.Text = "You heal " + TargetCreatureBase.Name + "! (+" + healAmt.ToString() + ")";
                myCharacter.AddMessage(healMsg1);
            }
            if (TargetCreatureBase is Character && TargetCreatureBase != myCreatureBase)
            {
                Character targetCharacter = (Character)TargetCreatureBase;

                Message healMsg1 = new Message();
                healMsg1.CreatureID = targetCharacter.CreatureID;
                healMsg1.Type = "spelldefense";
                healMsg1.Text = "You have been healed by " + myCreatureBase.Name + "! (+" + healAmt.ToString() + ")";
                targetCharacter.AddMessage(healMsg1);
            }
            foreach (Character character in CharactersInUpdateRange)
            {
                if (Math.Abs(character.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(character.MapY - TargetCreatureBase.MapY) <= 4)
                    character.UpdateCreatureBaseHp(TargetCreatureBase);

                if (Math.Abs(character.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(character.MapY - TargetCreatureBase.MapY) <= 8)
                    character.PlaySound("Heal", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
            }
            
        }
    }
}
